#include "widget.h"
#include "ui_widget.h"

#include <QMessageBox>

Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 下载定时器
    timerDownload = new QTimer(this);
    timerDownload->setInterval(100);

    // 拷贝定时器
    timerCopy = new QTimer(this);
    timerCopy->setInterval(100);

    connect(ui->btnDownload, &QPushButton::clicked, this, &Widget::onBtnStartDownload);
    connect(ui->btnCopy, &QPushButton::clicked, this, &Widget::onBtnStartCopy);

    connect(timerDownload, &QTimer::timeout, this, &Widget::onDownloadTimeout);
    connect(timerCopy, &QTimer::timeout, this, &Widget::onCopyTimeout);
}

Widget::~Widget()
{
    delete ui;
}

/** 开始下载
 * @brief Widget::onBtnStartDownload
 */
void Widget::onBtnStartDownload()
{
    // 启动定时器
    ui->pbDownload->reset();
    timerDownload->start();
}

/** 开始拷贝
 * @brief Widget::onBtnStartCopy
 */
void Widget::onBtnStartCopy()
{
    ui->pbCopy->reset();
    timerCopy->start();
}

// 下载定时器的回调
void Widget::onDownloadTimeout()
{
    int currentValue = ui->pbDownload->value();

    if(currentValue >= ui->pbDownload->maximum()) {
        timerDownload->stop();
        QMessageBox::information(this, "提示", "文件下载完成");
    } else {
        ui->pbDownload->setValue(currentValue + 1);
    }
}

void Widget::onCopyTimeout()
{
    int currentValue = ui->pbCopy->value();

    if(currentValue >= ui->pbCopy->maximum()) {
        timerCopy->stop();
        QMessageBox::information(this, "提示", "文件拷贝完成");
    } else {
        ui->pbCopy->setValue(currentValue + 1);
    }
}
